<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="talentPhone">
        <el-input
          v-model="queryParams.talentPhone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- 0代表未婚 1代表已婚 2表示单身 3代表未选择-->
      <el-form-item label="个人情况" prop="talentCondition">
        <el-input
          v-model="queryParams.talentCondition"
          placeholder="请输入个人情况"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- 0代表不在职 1代表在职 2代表未选择-->
      <el-form-item label="公司在职" prop="talentIncumbency">
        <el-input
          v-model="queryParams.talentIncumbency"
          placeholder="请输入是否在公司在职"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--<el-form-item label="简历文件名称" prop="fileName">-->
      <!--  <el-input-->
      <!--    v-model="queryParams.fileName"-->
      <!--    placeholder="请输入简历文件名称"-->
      <!--    clearable-->
      <!--    @keyup.enter.native="handleQuery"-->
      <!--  />-->
      <!--</el-form-item>-->
      <!--<el-form-item label="简历文件路径" prop="filePath">-->
      <!--  <el-input-->
      <!--    v-model="queryParams.filePath"-->
      <!--    placeholder="请输入简历文件路径"-->
      <!--    clearable-->
      <!--    @keyup.enter.native="handleQuery"-->
      <!--  />-->
      <!--</el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:talent:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:talent:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:talent:remove']"
        >删除</el-button>
      </el-col>

      <!--上传文件按钮-->
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-upload"
          size="mini"
          :disabled="single"
          @click="handleUpload"
          v-hasPermi="['system:talent:upload']"
        >上传</el-button>
      </el-col>

      <el-button
        class="upload-down"
        size="mini"
        type="primary"
        icon="el-icon-down"
        :disabled="multiple"
        @click="handleDownload"
        v-hasPermi="['system:info:down']"
      >下载</el-button>

      <!--<el-col :span="1.5">-->
      <!--  <el-button-->
      <!--    type="warning"-->
      <!--    plain-->
      <!--    icon="el-icon-download"-->
      <!--    size="mini"-->
      <!--    @click="handleExport"-->
      <!--    v-hasPermi="['system:talent:export']"-->
      <!--  >导出</el-button>-->
      <!--</el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="talentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--<el-table-column label="人才id" align="center" prop="id" />-->
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="性别" align="center" prop="talentSex" />
      <el-table-column label="联系电话" align="center" prop="talentPhone" />
      <!-- 0代表未婚 1代表已婚 2表示单身 3代表未选择-->
      <el-table-column label="个人情况" align="center" prop="talentCondition" :formatter="conditionFormat" />
      <!-- 0代表不在职 1代表在职 2代表未选择-->
      <el-table-column label="公司在职" align="center" prop="talentIncumbency" :formatter="incumbencyFormat" />
      <el-table-column label="文件名称" align="center" prop="fileName" />
      <el-table-column label="文件路径" align="center" prop="filePath" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <!--性别-->
        <el-form-item label="性别" prop="talentSex">
          <el-select v-model="form.talentSex" placeholder="请选择性别">
            <el-option label="男" value="男" />
            <el-option label="女" value="女" />
          </el-select>
        </el-form-item>
        <el-form-item label="联系电话" prop="talentPhone">
          <el-input v-model="form.talentPhone" placeholder="请输入联系电话" />
        </el-form-item>
        <!-- 0代表未婚 1代表已婚 2表示单身 3代表未选择-->
        <el-form-item label="个人情况" prop="talentCondition">
          <!-- 0代表未婚 1代表已婚 2表示单身 3代表未选择-->
          <!--<el-input v-model="form.talentCondition" placeholder="请输入个人情况" />-->
          <el-select v-model="form.talentCondition" placeholder="请选择个人情况">
            <el-option
              v-for="item in conditionList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <!-- 0代表不在职 1代表在职 2代表未选择-->
        <el-form-item label="公司在职" prop="talentIncumbency">
          <!-- 0代表不在职 1代表在职 2代表未选择-->
          <!--<el-input v-model="form.talentIncumbency" placeholder="请输入是否在公司在职" />-->
          <el-select v-model="form.talentIncumbency" placeholder="请选择是否在公司在职">
            <el-option
              v-for="item in incumbencyList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <!--<el-form-item label="文件名称" prop="fileName">-->
        <!--  <el-input v-model="form.fileName" placeholder="请输入简历文件名称" />-->
        <!--</el-form-item>-->
        <!--<el-form-item label="文件路径" prop="filePath">-->
        <!--  <el-input v-model="form.filePath" placeholder="请输入简历文件路径" />-->
        <!--</el-form-item>-->
        <!--简历文件-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>



    <!--上传简历信息对话框-->
    <el-dialog :title="title" :visible.sync="secondopen" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!--<el-form-item label="文件名称" prop="fileName">-->
        <!--  <el-input v-model="form.fileName" placeholder="请输入简历文件名称" />-->
        <!--</el-form-item>-->

        <!--选择文件按钮-->
        <el-form-item label="上传文件" prop="filePath">
          <!--
                      :headers="upload.headers"
          -->
          <el-upload
            class="upload-demo"
            ref="upload"
            :action="upload.url"
            :show-file-list="true"
            :file-list="upload.fileList"
            :auto-upload="false">
            <el-button size="small" type="primary">选择文件</el-button>
            <div slot="tip" class="el-upload__tip">格式支持docx，pdf</div>
          </el-upload>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="upload.isUploading" @click="submitUpload">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import {listTalent, getTalent, delTalent, addTalent, updateTalent, uploadResume, getTalents} from "@/api/system/talent";
import {getToken} from "@/utils/auth";
import {getFiles} from "@/api/system/file";

export default {
  name: "Talent",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      talentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      secondopen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        talentSex: null,
        talentPhone: null,
        talentCondition: null,
        talentIncumbency: null,
        fileName: null,
        filePath: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "姓名不能为空", trigger: "blur" },
          { min: 1, max: 20, message: "长度在 1 到 20 个字符", trigger: "blur" }
        ],
        talentSex: [
          { required: true, message: "性别不能为空", trigger: "change" }
        ],
        talentPhone: [
          { required: true, message: "联系电话不能为空", trigger: "blur" },
          { min: 11, max: 11, message: "长度为11字符", trigger: "blur" }
        ],
        talentCondition: [
          { required: true, message: "个人情况不能为空", trigger: "change" }
        ],
        talentIncumbency: [
          { required: true, message: "公司在职不能为空", trigger: "change" }
        ],
        fileName: [
          { required: true, message: "文件名称不能为空", trigger: "blur" },
          { min: 1, max: 20, message: "长度在 1 到 20 个字符", trigger: "blur" }
        ],
      },
      // 上传参数
      upload: {
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken(), 'Content-Type': 'multipart/form-data;boundary=----WebKitFormBoundary7TMYhSONfkAM2z3a'},
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/interview/talent/uploadResume",
        // 上传的文件列表
        fileList: []
      },
      conditionList:[{
        value: 0,
        label: '未婚'
      },{
        value: 1,
        label: '已婚'
      },{
        value: 2,
        label: '单身'
      },{
        value: 3,
        label: '未选择'
      }],
      incumbencyList:[{
        value: 0,
        label: '不在职'
      },{
        value: 1,
        label: '在职'
      },{
        value: 2,
        label: '未选择'
      }]
    };
  },
  created() {
    this.getList();
  },
  methods: {

    conditionFormat(row){
      if (row.talentCondition == 0) {
        return '未婚';
      } else if (row.talentCondition == 1) {
        return '已婚';
      } else if (row.talentCondition == 2) {
        return '单身';
      } else if (row.talentCondition == 3) {
        return '未选择';
      }
    },

    incumbencyFormat(row){
      if (row.talentIncumbency == 0) {
        return '不在职';
      } else if (row.talentIncumbency == 1) {
        return '在职';
      } else if (row.talentIncumbency == 2) {
        return '未选择';
      }
    },


    /** 多下载触发方法 包括单下载  */
    handleDownload(){
      /** 获取多选框选中的id  */
      const ids = this.ids;
      for (let i = 0;i <= ids.length-1; ++i){
        getTalents(ids[i]).then(response => {
          if(response.code == "200"){
            console.log(response.data);
            /** 文件名  */
            const name = response.data[0].fileName;
            /** 文件路径  */
            const url = response.data[0].filePath;
            /** 判断数据源是否为空  */
            if (url != null){
              /** 创建隐藏的可下载链接  */
              const suffix = url.substring(url.lastIndexOf("."), url.length);
              const a = document.createElement('a')
              /** 设置下载的文件名  */
              a.setAttribute('download', name + suffix)
              /** 设置在新窗口打开  */
              a.setAttribute('target', '_blank')
              /** 设置下载的路径  */
              a.setAttribute('href', url)
              /** 点击下载  */
              a.click()
            }
          }
        })
      }
    },


    select(){
      /** 只能选择一个复选框  */
      if (this.ids.length > 1) {
        this.$message({
          message: "只能选择一个复选框",
          type: "warning"
        });
        this.ids = [];
        return;
      }
    },



    /** 上传按钮方法  */
    handleUpload() {
      this.secondopen = true;
      this.title = "上传简历信息";
    },

    /** 上传窗口确定按钮触发方法  */
    submitUpload(){
      /** 获取多选框选中的id  */
      const ids = this.ids;
      // console.log(ids[0]);
      // /** 打印ids类型 */
      // console.log(typeof ids[0]);
      /** 获取选中按钮的选中的文件  */
      this.upload.fileList = this.$refs.upload.uploadFiles;
      // console.log(this.upload.fileList);
      const formData = new FormData();
      for (let i = 0; i < this.upload.fileList.length; i++) {
        formData.append('file', this.upload.fileList[i].raw);
      }

      uploadResume(formData,ids[0]).then(response => {
        // console.log("-------")
        // console.log(response);
        // debugger
        if(response.code == 200){
          this.$message({
            message: '上传成功',
            type: 'success'
          });
        }else {
          this.$message({
            message: '上传失败',
            type: 'error'
          });
        }
        this.secondopen = false;
        this.getList();
      });
    },


    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listTalent(this.queryParams).then(response => {
        this.talentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.secondopen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        talentSex: null,
        talentPhone: null,
        talentCondition: null,
        talentIncumbency: null,
        fileName: null,
        filePath: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加人才信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTalent(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改人才信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTalent(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTalent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除编号为"' + ids + '"的数据项？').then(function() {
        return delTalent(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/talent/export', {
        ...this.queryParams
      }, `talent_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>



